<?php

/**
 # com_jvframwork - JV Framework
 # @version		1.5.x
 # ------------------------------------------------------------------------
 # author    Open Source Code Solutions Co
 # copyright Copyright (C) 2011 joomlavi.com. All Rights Reserved.
 # @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL or later.
 # Websites: http://www.joomlavi.com
 # Technical Support:  http://www.joomlavi.com/my-tickets.html
-------------------------------------------------------------------------*/

// Check to ensure this file is within the rest of the framework
defined('JPATH_BASE') or die();

/**
 * Renders a list element
 *
 * @package 	Joomla.Framework
 * @subpackage	Parameter
 * @since		1.5
 */
 require_once (JPATH_COMPONENT .DS. 'libraries'. DS . 'addons'. DS . 'gfont' . DS . 'gfont.php');

class JElementGfont extends JElement {
    /**
     * Element type
     *
     * @access	protected
     * @var		string
     */
    var $_name = 'Gfont';
  
    function fetchElement($name, $value, &$node, $control_name) {
        
        $html = array();
        $params = $this->getParameter();
        
        $html[] = '<div>';
        $html[] = $this->getFontList($params);
        $html[] = $this->getApply($params);
        $html[] = '</div>';
        
        return implode("\n", $html);
    }
    
    function getFontList($params){                
        $fontlist = $params->get('gfont') != '' ? (array) $params->get('gfont') : Gfont::getFont();
        
        $html = array();
        $html[] = '<div id="font_list" class="google_font">';
        $html[] = '<h3 class="page_title">Font List</h3>';
        $html[] = '<ul class="menu accordion">';
        $fontArr = array();
        
        foreach($fontlist as $fScript => $fonts){
            $html[] = '<li class="parent">';
            $html[] = '<span class="separator"><span>'.str_replace('_', ' ', $fScript).'</span><span></span></span>';            
            //$html[] = '<a class="edit-button" onclick="editFontList(\''.strtolower($fScript).'\')" href="javascript:void(0);"></a>';
            $html[] = '<a class="del-button" onclick="this.getParent().remove();" href="javascript:void(0);"></a>';
            $html[] = '<ul class="fontlist">';
            
            $fonts = str_replace("\r\n", "\n", $fonts);
                        
            $_fonts = explode("\n", $fonts);
            if(is_array($_fonts)){
                foreach($_fonts as $_font){
                    $fontArr[] = str_replace(' ', '+', $_font);
                    $html[] = '<li>';
                    $html[] = '<span style="font-weight: normal; color: #000; font-family: \''.$_font.'\', '.($fScript == 'KHMER' ? 'cursive': 'serif').'"><label>'.trim($_font).'</label></span>';   
                    $html[] = '<span class="icon-refresh"></span><span class="icon-status inact '.str_replace(' ','-',trim($_font)).'"></span>';
                    $html[] = '</li>';
                }
            }
            
            $html[] = '</ul>';
            $html[] = '<textarea cols="43" rows="10" class="hidden" id="'.strtolower($fScript).'" name="params[gfont]['.$fScript.']">'.$fonts.'</textarea';
            $html[] = '</li>';
            
        }
        
        $html[] = '</ul>';
        $html[] = '<span class="item add"></span>';
        $html[] = '</div>';
        
        $document =& JFactory::getDocument();         
        $document->addScript('https://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js');    
        
        // Load JS Libraries
        JHTML::_('JVLib.loadJSlib', 
                    JURI::base()."components/com_jvframework/assets/js/jvlib/", // Lib Path
                    array(
                        'JVLib.GoogleFont', // Register GoogleFont
                        'JVLib.Drag'        // Register Drag
                    ), 
                    array(
                        JURI::base()."components/com_jvframework/models/fields/gfont.js" // External JS
                    )
        );
        
        return implode("\n", $html);
    }
    
    function getApply($params){
        $html = array();
        $fontSetup = $params->get('gfontapply') != '' ? (array)$params->get('gfontapply') : Gfont::getFontSettup();
        $_font = array();
        
        $html[] = '<div id="font_apply" class="google_font">';
        $html[] = '<h3 class="page_title">Apply to</h3>';
        $html[] = '<div class="inner_content clearfix">';
        $html[] = '<ul class="choose-font">';
        
        foreach($fontSetup as $key => $item){
            if(is_object($item)){
                $font = $item->font;
                $selector = $item->selector;
            }else{
                $font = '';
                $selector = $item;
            }
            
            if($font != ''){
               $_font[] = $font;
            }
            
            $html[] = '<li'.($font != '' ? ' class="fontapply selected" style="font-family: \''.$font.'\'"' :' class="fontapply"').'><span>'.$key.'<span class="selected_font">'.$font.'</span></span>';
            $html[] = '<a class="del-button3" onclick="this.getParent().remove();" href="javascript:void(0);"></a>';
            $html[] = '<a class="del-button2" onclick="this.getParent().setStyle(\'font-family\',\'\').removeClass(\'selected\'); this.getNext().value = \'\'; this.getParent().child()[0].child()[0].setText(\'\');" href="javascript:void(0);"></a>';        
            $html[] = '<input class="hidden fontval" type="text" name="params[gfontapply]['.$key.'][font]" value="'.$font.'" />'; 
            $html[] = '<input class="hidden" type="text" name="params[gfontapply]['.$key.'][title]" value="'.$key.'" />';            
            $html[] = '<textarea class="hidden" type="text" name="params[gfontapply]['.$key.'][selector]">'.$selector.'</textarea>';
            $html[] = '</li>';
        }
        
        $html[] = '</ul>';
        $html[] = '</div>';    
        $html[] = '<span class="item add"></span>';
        $html[] = '</div>'; 
        
        JFactory::getDocument()->addScriptDeclaration("
            window.addEvent('domready',function(){
                JVLib.GoogleFont.load(".json_encode($_font).");
            });
            
        ");
        
        
        return implode("\n", $html);
    }
    
    function getParameter(){
        $cids = JRequest::getVar('cid', array());                        
        $data =& JFactory::getDBO();        
        $data->setQuery('SELECT params FROM #__jv_themes WHERE id='.$cids[0]); 
        $resuft = $data->loadResult();
        
        return new JVParameter($resuft);
    }
    
    
    public function fetchTooltip($label, $description, $xmlElement, $control_name, $name) {
        return '';
    }
}